package com.work.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 *使用这个工具  需要注意两点
 * 1.新建一个Servlet类 必须以Servlet结尾
 * 2.把service方法直接复制到你的类中把方法名改成你的方法名
 * 3.把方法名的访问修饰符改为public
 * 4.访问地址就变成了  类名_方法名.action
 */
public class WorkServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //返回的url-pattern的值
        String servletPath = request.getServletPath();
//		System.out.println(servletPath);//   /User_list.do  /User_remove.do
        servletPath = servletPath.substring(1, servletPath.length() - 3);//User_list
        //获取调用的servlet类
        String className = servletPath.split("_")[0];
        //获取调用的servlet类的方法
        String methodName = servletPath.split("_")[1];
//		System.out.println(methodName);
//		System.out.println(className);
        //反射
        try {
<<<<<<< HEAD
            Class cls = Class.forName("com.work.servlet." + className + "Servlet");
=======
            Class cls = Class.forName("com.hmf.servlet." + className + "Servlet");
>>>>>>> 98bae14bd3a94d40343a0560834ad8f76ad2282b
            Object obj = cls.newInstance();
            Method method = cls.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(obj, request, response);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

}